$(function() {
	$('#manager').live('blur', function() {
		department.getUserUniqueId($(this).val());
	});
});

var department = (function(){
	
	var getUserUniqueId = function(username) {
		$.ajax({
			type: "GET",
			url: _siteUrl+"/HecUser/GetUniqueId/",
			data: "username="+username,
			dataType: 'json',
			success: function(data){
				$("#manager_unique_id").val(data.result);
			}
		});
	}
	
	var createOption = function(d,l) {
		$("#parent_id_"+l+" option").remove(); 
		html = '<option value="0">'+l+'级</option>';
		if(d)
		{
			$.each(d,function(){
				html += '<option value="'+this.id+'">'+this.name+'</option>';
			});
		}
		$("#parent_id_"+l).append(html);
	}
	
	var showDepartment = function(level) {
		var parent_id = $("#parent_id_"+level).val();
		if(parent_id==0) {
			$("#level").val(level);
		} else {
			level = level + 1;
			$("#level").val(level);
		}
		$.ajax({
			type: "GET",
			url: _siteUrl+"/UserDepartment/GetChildren/",
			data: "level="+level+"&parent_id="+parent_id,
			dataType: "JSON",
			success: function(data){
				department.createOption(data.result,level);
			}
		});
	}
	
	var _showError = function (e) {
		if(e == 1) {
			$("#notice").css("display","block");
			$("#notice").html("请填写调动说明！");
		} else if (e == 2) {
			$("#notice").css("display","block");
			$("#notice").html("请选择新部门！");
		} else if (e == 3) {
			$("#notice").css("display","block");
			$("#notice").html("请选择调动的元素！");
		} else if (e == 4) {
			$("#notice").css("display","block");
			$("#notice").html("请填写生效时间！");
		} 
	}
	 
	return {
		getUserUniqueId : getUserUniqueId,
		showDepartment : showDepartment,
		createOption : createOption
	};
 
})();